<?php


namespace app\admin\controller\order;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;

class Cardorder extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where[] = ['o.status', '<>', 1];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }
                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('mcard_order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->join('mcard mc', 'mc.id=o.mcard_id')
                ->field('o.*,mc.name as mc_name,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->order('o.pay_time desc')
                ->select();
            $total =Db::name('mcard_order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->join('mcard mc', 'mc.id=o.mcard_id')
                ->where($where)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($_list[$k]['pay_time']) {
                    $_list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
                } else {
                    $_list[$k]['pay_time'] = "------";
                }
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    public function refund()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $order_info=Db::name('mcard_order')->find($data['row']['id']);
            if ($data['row']['tui_price']<0){
                $this->error('请输入正确数值！');
            }
            if ($data['row']['tui_price']>$data['row']['pay_money']){
                $this->error('退款金额不能大于支付金额');
            }
            $config = config('wechat.wxpay');
            $app = Factory::payment($config);
            $result = $app->refund->byOutTradeNumber($order_info['order_no'], $order_info['order_no'], $order_info['pay_money'] * 100, $data['row']['tui_price'] * 100, [
                // 可在此处传入其他参数，详细参数见微信支付文档
                'refund_desc' => '会员卡退款',
            ]);
            if ($result['result_code'] == "FAIL") {
                $this->error($result['err_code_des']);
            }
            tuikuanMcardOrder($order_info, $data['row']['id'], 1);

            $this->success('操作成功！');

        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('mcard_order')->find($id);
        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch('');
    }

    public function refund2()
    {
        $order_id = $this->request->param('id/d', 0);
        Db::startTrans();
        try {
            $order_info = Db::name('mcard_order')->where('id', $order_id)->find();
            $config = config('wechat.wxpay');
            $app = Factory::payment($config);
            $result = $app->refund->byOutTradeNumber($order_info['order_no'], $order_info['order_no'], $order_info['pay_money'] * 100, $order_info['pay_money'] * 100, [
                // 可在此处传入其他参数，详细参数见微信支付文档
                'refund_desc' => '订单退款',
            ]);
            if ($result['result_code'] == "FAIL") {
                return ['code' => 100, 'data' => '', 'msg' => $result['err_code_des']];
            }
            tuikuanPackOrder($order_info, $order_id, 1);
            Db::commit();
            return ['code' => 200, 'data' => '', 'msg' => '操作成功'];
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => 100, 'data' => '', 'msg' => '操作失败' . $e->getMessage()];
        }
    }

    public function detail()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
        }
        $id = $this->request->param('id/d', 0);
        $order_info = Db::name('mcard_order')->find($id);
        $member_mcard_server_info = Db::name('member_mcard_server')->alias('mms')
            ->join('server s','s.id=mms.server_id')
            ->where([
                ['mms.mcard_list_id','=',$order_info['mcard_list_id']]
            ])
            ->field('mms.*,s.name as server_name,s.icon_img as icon_img')
            ->select();
        $this->assign([
            'order_info' => $order_info,
            'member_mcard_server_info'=>$member_mcard_server_info,
        ]);
        return $this->fetch();
    }
}